<!-- 购物卡结佣管理-购物卡类别-->
<template>
    <div class="shoppingCardType">
        <el-form :inline="true" :model="searchParams">
            <el-form-item label="类别名称：">
                <el-input v-model="searchParams.typeName" placeholder="请输入购物卡类别"></el-input>
            </el-form-item>
            <el-form-item label="操作人：">
                <el-input v-model="searchParams.updatorName" placeholder="请输入操作人姓名"></el-input>
            </el-form-item>
            <el-form-item label="操作时间：">
                <el-date-picker
                        v-model="searchParams.time"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch" id="monitorEnter">查询</el-button>
                <el-button @click="onClear">重置</el-button>
            </el-form-item>
        </el-form>

        <div class="mb20">
            <el-button type="primary" @click="handleShoppingCardType('new')">新建</el-button>
        </div>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
            <el-table-column align="center" prop="id" label="id" width="50"></el-table-column>
            <el-table-column align="center" prop="merchantName" label="商家名称"></el-table-column>
            <el-table-column align="center" prop="merchantIcon" width="200" label="商家图标">
                <template slot-scope="scope">
                    <img width="180" :src="scope.row.merchantIcon" alt="">
                </template>
            </el-table-column>
            <el-table-column align="center" prop="typeName" label="类别名称"></el-table-column>
            <el-table-column align="center" prop="remark" label="备注">
                <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" :content="scope.row.remark" placement="top">
                        <span>{{scope.row.remark | splitWord}}</span>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="updatorName" label="操作人"></el-table-column>
            <el-table-column align="center" prop="updateTime" width="100" label="操作时间"></el-table-column>
            <el-table-column align="center" label="操作" width="120">
                <template slot-scope="scope">
                    <a href="javascript:;;" class="handleA" @click="handleShoppingCardType('edit', scope.row)">编辑</a>
                    <a href="javascript:;;" class="handleA ml30" @click="handleShoppingCardType('del', scope.row)">删除</a>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagebar">
            <el-pagination
                    background
                    layout="total, sizes, prev, pager, next"
                    :current-page.sync="tableConfig.pageNum"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="tableConfig.pageSize"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :total="tableConfig.totalSize">
            </el-pagination>
        </div>

        <el-dialog :title="dialogFormTitle+'购物卡类别'" :visible.sync="dialogFormVisible" width="530px">
            <el-form :model="shoppingCardForm" ref="shoppingCardForm" :rules="shoppingCardFormRules">
                <el-form-item label="商家名称" prop="merchantName" label-width="95px">
                    <el-input v-model="shoppingCardForm.merchantName" maxlength="20" auto-complete="off" placeholder="请输入" class="w-320"></el-input>
                    <span class="tips">20字以内</span>
                </el-form-item>
                <el-form-item label="商家图标" prop="merchantIcon" label-width="95px">
                    <uploadImg @uploadSuccess="uploadSuccess" v-model="shoppingCardForm.merchantIcon"></uploadImg>
                    <span class="tips">格式支持jpg/png,建议尺寸X*X像素</span>
                </el-form-item>
                <el-form-item label="类别名称" prop="typeName" label-width="95px">
                    <el-input v-model="shoppingCardForm.typeName" maxlength="20" auto-complete="off" placeholder="请输入" class="w-320"></el-input>
                    <span class="tips">20字以内</span>
                </el-form-item>
                <el-form-item label="备注" label-width="95px">
                    <el-input type="textarea" maxlength="100" :autosize="{ minRows: 3, maxRows: 7}" v-model="shoppingCardForm.remark" auto-complete="off" placeholder="请输入" class="w-320"></el-input>
                    <div class="w-320" style="text-align: right">
                        {{shoppingCardForm.remark.length}}/{{100-shoppingCardForm.remark.length}}
                    </div>
                    <el-input v-model="shoppingCardForm.id" type="hidden"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="saveShoppingCardForm">保 存</el-button>
                <el-button @click="dialogFormVisible = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
    import utils from "@/common/utils.js";
    import uploadImg from "./uploadImg"
    import api from '@/api/apiPath'

    export default {
        name: 'shoppingCardType',
        title: '购物卡类型',
        data() {
            return {
                searchParams: {
                    time: ["", ""]
                },
                tableData: [],
                tableConfig: {
                    pageSize: 10,
                    pageNum: 1,
                    totalSize: 10
                },
                shoppingCardForm: {
                    id: '',
                    typeName: "",
                    merchantName: "",
                    merchantIcon: "",
                    remark: "",
                },
                shoppingCardFormRules: {
                    merchantName: [
                        {required: true, message: '请输入商家名称', trigger: 'blur'},
                    ],
                    typeName: [
                        {required: true, message: '请输入类别名称', trigger: 'blur'},
                    ],
                    merchantIcon: [
                        {required: true, message: '请上传商家图标', trigger: 'blur'},
                    ],
                },
                dialogFormVisible: false,
                dialogFormTitle: "新增",
                imageUrl: ""
            }
        },
        created() {
            this.getTableList()
        },
        activated(){
            this.onSearch()
        },
        mounted() {

        },
        methods: {
            uploadSuccess(img) {
                console.log(img);
                this.shoppingCardForm.merchantIcon = img
            },
            handleSizeChange(size) {
                this.tableConfig.pageSize = size
                this.getTableList()
            },
            handleCurrentChange(page) {
                this.tableConfig.pageNum = page
                this.getTableList()
            },
            getTableList() {
                this.axios.post(api.couponShoppingCardTypeGetList, {
                    pageNum: this.tableConfig.pageNum,
                    pageSize: this.tableConfig.pageSize,
                    typeName: this.searchParams.typeName,
                    updatorName: this.searchParams.updatorName,
                    beginTime: utils.getTimeString(this.searchParams.time[0]),
                    endTime: utils.getTimeString(this.searchParams.time[1])
                }).then((res) => {
                    this.tableData = res.data.data.list;
                    this.tableConfig.totalSize = res.data.data.total;
                })
            },
            onSearch() {
                this.tableConfig.pageNum = 1;
                this.getTableList();
            },
            onClear() {
                this.searchParams = {
                    time: ["", ""]
                };
            },
            handleShoppingCardType(type, row) {
                if (type === 'new') {
                    this.dialogFormTitle = "新增";
                    this.shoppingCardForm = {
                        id: '',
                        typeName: "",
                        merchantName: "",
                        merchantIcon: "",
                        remark: "",
                    }
                    this.dialogFormVisible = true
                } else if (type === 'edit') {
                    this.dialogFormTitle = "编辑";
                    this.shoppingCardForm = {
                        merchantName: row.merchantName,
                        merchantIcon: row.merchantIcon,
                        typeName: row.typeName,
                        remark: row.remark || '',
                        id: row.id
                    }
                    this.dialogFormVisible = true
                } else {
                    this.$confirm('确定要删除吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.axios.post(api.couponShoppingCardTypeDelete, {
                            id: row.id
                        }).then((res) => {
                            if(res.data.code){
                                this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                this.getTableList()
                            }
                        })
                    })
                }

            },
            saveShoppingCardForm() {
                this.$refs["shoppingCardForm"].validate((valid) => {
                    if (valid) {
                        let url = api.couponShoppingCardTypeAdd;
                        if (this.shoppingCardForm.id) {
                            url = api.couponShoppingCardTypeEdit
                        }
                        this.axios.post(url, this.shoppingCardForm).then((res) => {
                            if (res.data.code) {
                                this.$message({
                                    message: this.dialogFormTitle + '成功',
                                    type: 'success'
                                });
                                this.dialogFormVisible = false;
                                this.getTableList()
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
        },
        components: {
            uploadImg
        },
        computed: {},
        filters: {
            splitWord(value) {
                if (value) {
                    return value.length > 30 ? (value.slice(0, 28) + "...") : value
                }
                return value
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="stylus">
    .shoppingCardType {
        .handleA {
            text-decoration: none;
        }

        .tips {
            color: #aaa;
        }
        .img-wrapper > i {
            //display: none;
        }

    }

</style>
